using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
using TCDbService;
using TCEntity;

[assembly: ApiController]
namespace TimeCameraServer.Controllers
{
  [Route("[controller]")]
  public class AccountController : FailureController
  {
    readonly IServiceProvider _provider;
    readonly AccountService _accountService;
    public AccountController(IServiceProvider service)
    {
      _provider = service;
      _accountService = _provider.GetService<AccountService>();
    }
    [HttpPut("login")]
    public async ValueTask<IActionResult> AuthAsync(User user) => await FailureProcess(async () =>
    {
      await _accountService.LoginAsync(user);
      return NoContent();
    });
    [HttpPost("register")]
    public async ValueTask<IActionResult> RegisterAsync(User user) => await FailureProcess(async () =>
    {
      await _accountService.RegisterAsync(user);
      return NoContent();
    });
    [HttpPost("changepswd")]
    public async ValueTask<IActionResult> ChangePswdAsync((User user, string password) message) => await FailureProcess(async () =>
    {
      await _accountService.ChangePswdAsync(message.user, message.password);
      return NoContent();
    });
    [HttpDelete("delete")]
    public async ValueTask<IActionResult> DeleteAsync(User user) => await FailureProcess(async () =>
    {
      await _provider.GetService<ShareService>().DeleteAsync(user);
      await _provider.GetService<CommunityService>().DeleteAsync(user);
      await _provider.GetService<ImageService>().DeleteAsync(user);
      await _accountService.DeleteAsync(user);
      return NoContent();
    });
  }
}
